package org.noka.http.header;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nokatag.system.Concest;
import org.nokatag.system.RequestUtils;

/**
 * 设置协议头
 * @author rebin
 *
 */
public class HttpHeaderSet {

	/**
	 * 设置协议头
	 * @param response
	 */
	public void setHeader(HttpServletRequest request,HttpServletResponse response){
		String resourcePath = RequestUtils.getServletPath(request);
        if ("".equals(resourcePath) && null != request.getPathInfo()) {
            resourcePath = request.getPathInfo();
        }
        //----------------是否适用私有协议头-------------------------------------------
        for(HeaderPrivateItem hp:Concest.HTTP_HEADER_PRIVATES){
        	if(resourcePath.startsWith(hp.getDir())){//有私有协议头
        		if(!hp.getForpublic()){//不需经继承公有协议
        			setHeaderValue(response,hp.getHeaders());//设置私有协议头
        		}else{
        			setHeaderValue(response,Concest.HTTP_HEADER_PUBLICS);//设置公共协议头
        			setHeaderValue(response,hp.getHeaders());//设置私有协议头
        		}
        		return;
        	}
        }
        //---------------设置公共协议头----------------------------------------------
        setHeaderValue(response,Concest.HTTP_HEADER_PUBLICS);//设置公共协议头
	}
	/**
	 * 设置协议头
	 * @param response
	 * @param headers
	 */
	private void setHeaderValue(HttpServletResponse response,List<HeaderItem> headers){
		for(HeaderItem header : headers){
			response.setHeader(header.getKey(), header.getValue());
		}
	}
}
